//
//  UIViewController+Extension.m
//  EMC
//
//  Created by apple on 2017/3/11.
//  Copyright © 2017年 sangcixiang. All rights reserved.
//

#import "UIViewController+Extension.h"


@implementation UIViewController (Extension)


/** 获取图片 */
- (void)openImagePickerWithTarget:(id)target withType:(UIImagePickerControllerSourceType)type editing:(Boolean)editing {
    
    UIImagePickerController *imagePicker = [UIImagePickerController new];
    imagePicker.sourceType = type;
    imagePicker.delegate = target;
    imagePicker.allowsEditing = editing;
    [self presentViewController:imagePicker animated:YES completion:nil];
    
}

-(void)openAlertControllerTitle:(NSString *)title message:(NSString *)message style:(UIAlertControllerStyle)style actionTitle1:(NSString *)actionTitle1 actionTitle2:(NSString *)actionTitle2 callback:(void (^)(int))callback{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:style];
    if (actionTitle1) {
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:actionTitle1 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            callback(1);
        }];
        [alert addAction:action1];
    }
    if (actionTitle2) {
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:actionTitle2 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            callback(2);
        }];
        [alert addAction:action2];
    }
    UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        callback(0);
    }];
    
    
    if (style == UIAlertControllerStyleActionSheet) {
        [alert addAction:action0];
    }
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)openImagePickerWithTaget:(id)taget withType:(UIImagePickerControllerSourceType)type editing:(Boolean)editing{
    UIImagePickerController *imagePicker = [UIImagePickerController new];
    imagePicker.sourceType = type;
    imagePicker.delegate = taget;
    imagePicker.allowsEditing = editing;
    [self presentViewController:imagePicker animated:YES completion:nil];
}


- (int)networkingStatesFromStatebar {
    // 状态栏是由当前app控制的，首先获取当前app
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    int type = 0;
    for (id child in children) {
        if ([child isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            type = [[child valueForKeyPath:@"dataNetworkType"] intValue];
        }
    }
    // 0==没有网络，1==2G，2==3G,3==4G,4==LTE,5==WIFI
    return type;
}

-(void)pushViewController:(UIViewController *)viewController{
    [self.navigationController pushViewController:viewController animated:YES];
}

-(void)popViewController{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
